package algorithm.middle;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import java.util.HashMap;

public class CountTexts2266 {

    private static long[] normal = new long[100001];
    private static long[] others = new long[100001];
    private static int c = 1000000007;

    static {
        //初始化数据
        normal[1] = 1;
        normal[2] = 2;
        normal[3] = 4;
        normal[4] = 7;
        others[1] = 1;
        others[2] = 2;
        others[3] = 4;
        others[4] = 8;
        //填充数字
        for (int i = 5; i < 100001; i++) {
            normal[i] = (normal[i - 1]% c + normal[i - 2]% c + normal[i - 3]% c) % c;
            others[i] = (others[i - 1]% c + others[i - 2]% c + others[i - 3]% c + others[i - 4]% c) % c;
        }
    }


    /**
     * c=1,n=1
     * c=2,n=2 =>  D[i-1]+D[i-2]
     * c=3, n=4 =>  D[i-1]+D[i-2]+D[i-2]
     * 7&9 c=4, n=8 =>  D[i-1]+D[i-2]+D[i-2]+D[i-3]
     * c>=5, n=15 =>  D[i-1]+D[i-2]+D[i-2]+D[i-3]
     * other c>=4,n= D[i-1]+D[i-2]+D[i-2]
     */

    public int countTexts(String pressedKeys) {
        int length = pressedKeys.length();
        char cur = pressedKeys.charAt(0);
        int count = 1;
        int res = 1;
        for (int i = 1; i < length; i++) {
            if (pressedKeys.charAt(i) == cur) {
                count++;
            } else {
                //并非相同数字
                Long aLong = getNum(cur- 48, count);
                count = 1;
                cur = pressedKeys.charAt(i);
                res = (int) ((aLong % c) * res % c);
            }
        }
        if (count>1){
            int num = pressedKeys.charAt(length-1) - 48;
            Long aLong = getNum(num, count);
            res = (int) ((aLong % c) * res % c);
        }
        return res;
    }

    private Long getNum(int num, int len) {
        if (num == 7 || num == 9) {
            return others[len];
        } else {
            return normal[len];
        }
    }

    public static void main(String[] args) {
        CountTexts2266 obj = new CountTexts2266();
        System.out.println(obj.countTexts("55555555999977779555"));
        System.out.println(obj.countTexts("55555555"));
        System.out.println(obj.countTexts("9999"));
        System.out.println(obj.countTexts("7777"));
        System.out.println(obj.countTexts("9"));
        System.out.println(obj.countTexts("555"));



    }
}
